package com.cspg.ssss.service;

import com.alibaba.fastjson.JSONObject;
import com.cspg.ssss.config.WeixinProperties;
import com.cspg.ssss.util.HttpRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
public class WeixinService {
    private static final Logger log = LoggerFactory.getLogger(WeixinService.class);

    @Autowired
    WeixinProperties wxProperties;
    /**
     * 获取access_token
     * @param corpid 企业微信ID
     * @param corpsecret 企业应用Secret
     * @return
     */
    @Cacheable(value = "weixin", key="#corpsecret")
    public String getToken(String corpid, String corpsecret) {
        log.info("进入方法getToken，设置access_token缓存");
        String requestURL =  wxProperties.getUrlroot()+"/cgi-bin/gettoken?corpid=" + corpid + "&corpsecret=" + corpsecret;
        log.debug("获取微信access_token请求 = " + requestURL);
        JSONObject tokenObj = HttpRequest.httpsGetRequest(requestURL);
        log.debug("获取微信access_token响应 = "+ tokenObj.toJSONString());

        if( !tokenObj.getString("errcode").equals("0"))
        {
            log.error("获取access_token失败："+ tokenObj.toJSONString());
            return null;
        }
        return tokenObj.getString("access_token");
    }

    /**
     * 获取企业应用jsapi_ticket
     * @param token 根据企业ID和企业应用Secret获取到的access_token
     * @return
     */
    @Cacheable(value = "weixin",key="#token")
    public String getTicket(String token) {
        log.info("进入方法getToken，设置jsapi_ticket缓存");
        String requestURL = wxProperties.getUrlroot()+"/cgi-bin/get_jsapi_ticket?access_token=" + token;
        log.debug("获取企业应用jsapi_ticket请求 = " + requestURL);
        JSONObject eTicket = HttpRequest.httpsGetRequest(requestURL);
        log.debug("获取企业应用jsapi_ticket响应 = "+ eTicket.toJSONString());
        if( !eTicket.getString("errcode").equals("0"))
        {
            log.error("获取企业应用jsapi_ticket失败："+ eTicket.toJSONString());
            return null;
        }
        return eTicket.getString("ticket");
    }

    /**
     * 获取企业应用jsapi_ticket
     * @param token 根据企业ID和企业应用Secret获取到的access_token
     * @return
     */
    @Cacheable(value = "weixin",key="#agentId")
    public String getAppTicket(String agentId ,String token) {
        log.info("进入方法getToken，设置jsapi_ticket缓存");
        String requestURL = wxProperties.getUrlroot()+"/cgi-bin/ticket/get?access_token=" + token+"&type=agent_config";
        log.debug("获取企业应用agentConfig所需jsapi_ticket请求 = " + requestURL);
        JSONObject eTicket = HttpRequest.httpsGetRequest(requestURL);
        log.debug("获取企业应用agentConfig所需jsapi_ticket响应 = "+ eTicket.toJSONString());
        if( !eTicket.getString("errcode").equals("0"))
        {
            log.error("获取企业应用jsapi_ticket失败："+ eTicket.toJSONString());
            return null;
        }
        return eTicket.getString("ticket");
    }

    /**
     * 获取用户ID
     * @param accessToken 调用接口凭证
     * @param code 用户code
     * @return
     */
    public String getUserId(String accessToken, String code) {
        log.debug("进入获取微信用户信息");
        // https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token=ACCESS_TOKEN&code=CODE
        String requestURL = wxProperties.getUrlroot()+"/cgi-bin/user/getuserinfo?access_token="+accessToken+"&code="+code;
        log.debug("获取企业用户ID请求 = " + requestURL);
        JSONObject resObject = HttpRequest.httpsGetRequest(requestURL);
        log.debug("获取企业用户ID响应 = "+ resObject.toJSONString());
        if( !resObject.getString("errcode").equals("0"))
        {
            log.error("获取企业用户ID失败："+ resObject.toJSONString());
            return null;
        }
        return resObject.getString("UserId");
    }
}
